package ywpt.web.action.people;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.web.BaseAction;
import ywpt.model.PeopleApplyInfo;
import ywpt.model.SysMsgInfo;
import ywpt.service.people.PeopleApplyInfoService;
import ywpt.service.people.PeopleService;
import ywpt.service.work.SysMsgInfoService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

public class PeopleApplyInfoAction extends BaseAction implements ModelDriven<PeopleApplyInfo>,Preparable{

	/**
	 * 人力资源申请
	 */
	private static final long serialVersionUID = -5513884572396534701L;
	private static final int DEFAULT_PAGE_SIZE = 100;	
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];	
	private Long id;
	private PeopleApplyInfo peopleApplyInfo;
	private List<PeopleApplyInfo> peopleApplyInfoList = new ArrayList<PeopleApplyInfo>();
	private PeopleApplyInfoService peopleApplyInfoService;
	
	private int version;
	
	private Long applyId;
	private String applyName;
	private Long typeId;
	private String positionName;
	private Long positionId;
	private Long checkerId;
	private String checkerName;
	
	private PeopleService peopleService;
	
	private SysMsgInfo sysMsgInfo;
	private List<SysMsgInfo> sysMsgInfoList = new ArrayList<SysMsgInfo>();
	private SysMsgInfoService sysMsgInfoService;
	
	public List<SysMsgInfo> getSysMsgInfoList() {
		return sysMsgInfoList;
	}

	public void setSysMsgInfoList(List<SysMsgInfo> sysMsgInfoList) {
		this.sysMsgInfoList = sysMsgInfoList;
	}

	public SysMsgInfo getSysMsgInfo() {
		return sysMsgInfo;
	}

	public void setSysMsgInfo(SysMsgInfo sysMsgInfo) {
		this.sysMsgInfo = sysMsgInfo;
	}

	public SysMsgInfoService getSysMsgInfoService() {
		return sysMsgInfoService;
	}

	public void setSysMsgInfoService(SysMsgInfoService sysMsgInfoService) {
		this.sysMsgInfoService = sysMsgInfoService;
	}

	public PeopleService getPeopleService() {
		return peopleService;
	}

	public void setPeopleService(PeopleService peopleService) {
		this.peopleService = peopleService;
	}

	public Long getApplyId() {
		return applyId;
	}

	public void setApplyId(Long applyId) {
		this.applyId = applyId;
	}

	public String getApplyName() {
		return applyName;
	}

	public void setApplyName(String applyName) {
		this.applyName = applyName;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public String getPositionName() {
		return positionName;
	}

	public void setPositionName(String positionName) {
		this.positionName = positionName;
	}

	public Long getPositionId() {
		return positionId;
	}

	public void setPositionId(Long positionId) {
		this.positionId = positionId;
	}

	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public PeopleApplyInfo getPeopleApplyInfo() {
		return peopleApplyInfo;
	}

	public void setPeopleApplyInfo(PeopleApplyInfo peopleApplyInfo) {
		this.peopleApplyInfo = peopleApplyInfo;
	}

	public List<PeopleApplyInfo> getPeopleApplyInfoList() {
		return peopleApplyInfoList;
	}

	public void setPeopleApplyInfoList(List<PeopleApplyInfo> peopleApplyInfoList) {
		this.peopleApplyInfoList = peopleApplyInfoList;
	}

	public PeopleApplyInfoService getPeopleApplyInfoService() {
		return peopleApplyInfoService;
	}

	public void setPeopleApplyInfoService(
			PeopleApplyInfoService peopleApplyInfoService) {
		this.peopleApplyInfoService = peopleApplyInfoService;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public Long getCheckerId() {
		return checkerId;
	}

	public void setCheckerId(Long checkerId) {
		this.checkerId = checkerId;
	}

	public String getCheckerName() {
		return checkerName;
	}

	public void setCheckerName(String checkerName) {
		this.checkerName = checkerName;
	}
	/*-----------------------------------------------------------------------------------------------------*/
	@Override
	public PeopleApplyInfo getModel() {
		return this.peopleApplyInfo;
	}

	@Override
	public void prepare() throws Exception {
		if (id!=null) {
			this.peopleApplyInfo = this.peopleApplyInfoService.getPeopleApplyInfoById(id);
		} else {
			this.peopleApplyInfo = new PeopleApplyInfo();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper, popedom, OperationConstants.PEOPLE_APPLY);			
		
	}
	
	protected void getDateList(DetachedCriteria  query) {
		query.createAlias("creater", "creater").add(Restrictions.eq("creater.id", this.getCurrentUser().getId()));
		query.add(Restrictions.eq("deleteFlag", false));
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = peopleApplyInfoService.getPeopleApplyInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.desc("id"));
		query.setProjection(null);
		peopleApplyInfoList.addAll(peopleApplyInfoService.getPeopleApplyInfos(query, pageSize, rowStartEnd[0]));		
	}
	
	protected void showInfo() {
		if (this.peopleApplyInfo.getApplyUser()!=null) {
			this.applyId = this.peopleApplyInfo.getApplyUser().getId();
			this.applyName = this.peopleApplyInfo.getApplyUser().getPeopleName();
		}
		if (this.peopleApplyInfo.getApplyType()!=null) {
			this.typeId = this.peopleApplyInfo.getApplyType().getId();
		}
		if (this.peopleApplyInfo.getPosition()!=null) {
			this.positionId = this.peopleApplyInfo.getPosition().getId();
			this.positionName = this.peopleApplyInfo.getPosition().getPositionName();
		}
		if (this.peopleApplyInfo.getChecker()!=null) {
			this.checkerId = this.peopleApplyInfo.getChecker().getId();
			this.checkerName = this.peopleApplyInfo.getChecker().getPeopleName();
		}
		this.sysMsgInfoList.addAll(this.sysMsgInfoService.getMsgs(1, peopleApplyInfo.getId()));
	}
/*====================================================================================================*/
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(PeopleApplyInfo.class);
		getDateList(query);
		return SUCCESS;
	}
	
	public String input() {
		this.applyId= this.getCurrentUser().getPeople().getId();
		this.applyName=this.getCurrentUser().getPeople().getPeopleName();

		if (this.getCurrentUser().getPeople().getChecker()!=null) {
			this.checkerId = this.getCurrentUser().getPeople().getChecker().getId();
			this.checkerName = this.getCurrentUser().getPeople().getChecker().getPeopleName();
		}
		return INPUT;
	}
	
	public String edit() {
		this.peopleApplyInfo = this.peopleApplyInfoService.getPeopleApplyInfoById(id);
		showInfo();
		return INPUT;
	}

	public String save(){
		try {
			if(id==null)
			{   
				peopleApplyInfo.setCreater(this.getCurrentUser());
				peopleApplyInfo.setCreateDate(new Date());
				peopleApplyInfoService.savePeopleApplyInfo(peopleApplyInfo,applyId,typeId,positionId,checkerId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
				return SUCCESS;
			}
			else
			{
				this.peopleApplyInfo = this.peopleApplyInfoService.getPeopleApplyInfoById(id);
					if(!peopleApplyInfo.getCheckFlag()){
						if (!peopleApplyInfo.getSubmitFlag()) {
							if(this.getVersion()==peopleApplyInfo.getVersion())
							{
								peopleApplyInfo.setModifyer(this.getCurrentUser());
								peopleApplyInfo.setModifyDate(new Date());
								peopleApplyInfoService.updatePeopleApplyInfo(peopleApplyInfo,applyId,typeId,positionId,checkerId);
								showInfo();
								this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
								return SUCCESS;
							}else{
								showInfo();
								this.addActionMessage(SystemMessage.UPDATE_FAIL);
								return SUCCESS;
							}
						} else {
							showInfo();
							this.addActionMessage(SystemMessage.SUBMIT_SAVE);
							return SUCCESS;
						}
					}else{
						showInfo();
						this.addActionMessage(SystemMessage.AUDITING_SAVE);
						return SUCCESS;
					}	
			}
		} catch (Exception e) {
			this.addActionMessage(SystemMessage.SAVE_FAIL+e.getMessage());
			return SUCCESS;
		}		
	}
	
	public String delete() {
		if(id==null){
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}else{
			this.peopleApplyInfo = this.peopleApplyInfoService.getPeopleApplyInfoById(id);
			
			if(!peopleApplyInfo.getCheckFlag()){
				if (!peopleApplyInfo.getSubmitFlag()) {
					peopleApplyInfo.setDeleteFlag(true);
					peopleApplyInfoService.updatePeopleApplyInfo(peopleApplyInfo,null,null,null,null);
					sysMsgInfoService.invalidMsg( 1, id);
					return SUCCESS;					
				} else {
					showInfo();
					this.addActionMessage(SystemMessage.SUBMIT_AUDITING_FAIL);
					return INPUT;
				}				
			}else{
				showInfo();
				this.addActionMessage(SystemMessage.DELETE_AUDITING_FAIL);
				return INPUT;
			}
			
		}
	}


	public String submit() {
		if(id==null){
			this.addActionMessage(SystemMessage.NO_OBJECT);
		}else{
			this.peopleApplyInfo = this.peopleApplyInfoService.getPeopleApplyInfoById(id);
			if (this.checkerId!=null) {
				if(!peopleApplyInfo.getSubmitFlag()){
					peopleApplyInfo.setSubmitFlag(true);
					peopleApplyInfoService.updatePeopleApplyInfo(peopleApplyInfo,null,null,null,checkerId);
					sysMsgInfoService.invalidMsg( 1, id);
					addSysMsg(id,checkerId);
					this.addActionMessage(SystemMessage.SUMBIT_SUCCESS);
				}else{
					this.addActionMessage(SystemMessage.SUMBIT_REPEAT);
				}
			} else {
				this.addActionMessage("请添加审核人！");
			}
		}
		showInfo();
		return INPUT;
	}
	
	protected void addSysMsg(Long id , Long checkerId) {
		SysMsgInfo sysMsgInfo = new SysMsgInfo();
		sysMsgInfo.setMsgTitle("人事申请");
		sysMsgInfo.setMsgContent(this.getCurrentUser().getPeople().getPeopleName()+"提交了一条人事申请信息！");
		sysMsgInfo.setReceiveUser(checkerId +",");
		sysMsgInfo.setBusinessType(1);
		sysMsgInfo.setBusinessId(id);
		sysMsgInfo.setUrl("/people/checkapply/edit-"+id+".action");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfo.setCreater(this.getCurrentUser());
		sysMsgInfoService.saveSysMsgInfo(sysMsgInfo);
	}
}
